package com.hc.javabasestudyproject.feature.stream;

import com.hc.javabasestudyproject.feature.entity.Author;
import com.hc.javabasestudyproject.feature.entity.DataUtils;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: huangcheng
 * @CreateTime: 2023-11-22  16:30
 * @Description:
 * @Version: 1.0
 */
public class SortedStream {
    public static void main(String[] args) {
        List<Author> dataList = DataUtils.getAuthors();
        List<String> list = Arrays.asList("aa", "ff", "dd");
        //String 类自身已实现Compareable接口
        list.stream().sorted().forEach(System.out::println);
        //使用默认排序，需要实现Compareble接口，重写comparetor方法
        List<Author> collect = dataList.stream()
                .sorted()
                .collect(Collectors.toList());
        collect.forEach(System.out ::println);
        //Comparator自定义排序规则1
        List<Author> collect1 = dataList.stream()
                .sorted(Comparator.comparing(Author::getAge))
                .collect(Collectors.toList());
        collect1.forEach(System.out ::println);
        //Comparator多重排序比较
        List<Author> collect2 = dataList.stream()
                .sorted((o1, o2) -> {
                    if (o1.getName().equals(o2.getName())) {
                        return o1.getAge() - o2.getAge();
                    } else {
                        return o1.getName().compareTo(o2.getName());
                    }
                })
                .collect(Collectors.toList());
        collect2.forEach(System.out ::println);
    }
}
